package com.server.modules.sys.utils.compress;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
 
import com.github.junrar.Archive;
import com.github.junrar.exception.RarException;
import com.github.junrar.rarfile.FileHeader;
 
/**
*@author www.zuidaima.com
**/
public class ExtractUtils {
	public  static String root = "d:/test/compress";
    public static boolean extractZipFiles(File file) {
        try {
        	if(!file.getName().contains(".zip")){
        		return false;
        	}
        	root=file.getParentFile().getAbsolutePath();
            byte[] buf = new byte[1024];
            String fileName = file.getName();
            String[] split = fileName.split("\\.");
            fileName=split[0];
            ZipInputStream zipinputstream = null;
            ZipEntry zipentry;
            zipinputstream = new ZipInputStream(new FileInputStream(file));
            zipentry = zipinputstream.getNextEntry();
            while (zipentry != null) {
                // for each entry to be extracted
                String entryName = zipentry.getName();
                System.out.println(entryName);
                int n;
                FileOutputStream fileoutputstream;
                File newFile = new File(entryName);
                String directory = newFile.getParent();
                // to creating the parent directories
                if (directory == null) {
                    if (newFile.isDirectory()) {
                        break;
                    }
                } else {
                    new File(root +File.separator+fileName+File.separator+ directory).mkdirs();
                }
                if (!zipentry.isDirectory()) {
                    System.out.println("File to be extracted....." + entryName);
                    new File(root +File.separator+fileName+File.separator+ directory).mkdirs();
                    fileoutputstream = new FileOutputStream(root+File.separator+fileName+File.separator + entryName);
                    while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
                        fileoutputstream.write(buf, 0, n);
                    }
                    fileoutputstream.close();
                }
 
                zipinputstream.closeEntry();
                zipentry = zipinputstream.getNextEntry();
            }
            zipinputstream.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    public static boolean extractRarFiles(File archive) {
    	 try {
    			root=archive.getParentFile().getAbsolutePath();
            	System.out.println(root);
    	String name = archive.getName();
    	String[] split = name.split("\\.");
    	if(!name.contains(".rar")){
    		return false;
    	}
    	  File destination = new File(root+File.separator+split[0]);
    	  destination.mkdirs();
    	  Archive arch = null;
       
            arch = new Archive(archive);
       
        if (arch != null) {
            if (arch.isEncrypted()) {
                System.out.println("archive is encrypted cannot extreact");
                return false;
            }
            FileHeader fh = null;
            while (true) {
                fh = arch.nextFileHeader();
                if (fh == null) {
                    break;
                }
                if (fh.isEncrypted()) {
                    System.out.println("file is encrypted cannot extract: "
                            + fh.getFileNameString());
                    continue;
                }
                System.out.println("extracting: " + fh.getFileNameString());
                try {
                    if (fh.isDirectory()) {
                        createDirectory(fh, destination);
                    } else {
                        File f = createFile(fh, destination);
                        OutputStream stream = new FileOutputStream(f);
                        arch.extractFile(fh, stream);
                        stream.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return true;
        }
    	 } catch (RarException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
    	 return false;
    }
 
    private static File createFile(FileHeader fh, File destination) {
        File f = null;
        String name = null;
        if (fh.isFileHeader() && fh.isUnicode()) {
            name = fh.getFileNameW();
        } else {
            name = fh.getFileNameString();
        }
        f = new File(destination, name);
        if (!f.exists()) {
            try {
                f = makeFile(destination, name);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return f;
    }
 
    private static File makeFile(File destination, String name) throws IOException {
        String[] dirs = name.split("\\\\");
        if (dirs == null) {
            return null;
        }
        String path = "";
        int size = dirs.length;
        if (size == 1) {
            return new File(destination, name);
        } else if (size > 1) {
            for (int i = 0; i < dirs.length - 1; i++) {
                path = path + File.separator + dirs[i];
                new File(destination, path).mkdir();
            }
            path = path + File.separator + dirs[dirs.length - 1];
            File f = new File(destination, path);
            f.createNewFile();
            return f;
        } else {
            return null;
        }
    }
 
    private static void createDirectory(FileHeader fh, File destination) {
        File f = null;
        if (fh.isDirectory() && fh.isUnicode()) {
            f = new File(destination, fh.getFileNameW());
            if (!f.exists()) {
                makeDirectory(destination, fh.getFileNameW());
            }
        } else if (fh.isDirectory() && !fh.isUnicode()) {
            f = new File(destination, fh.getFileNameString());
            if (!f.exists()) {
                makeDirectory(destination, fh.getFileNameString());
            }
        }
    }
 
    private static void makeDirectory(File destination, String fileName) {
        String[] dirs = fileName.split("\\\\");
        if (dirs == null) {
            return;
        }
        String path = "";
        for (String dir : dirs) {
            path = path + File.separator + dir;
            new File(destination, path).mkdir();
        }
    }
 
    public static void main(String[] args) {
    	ExtractUtils main = new ExtractUtils();
        // extract zip
      //  main.extractZipFiles();
        // extract rar
       // File file=new File("d://test//compress");
    	//File file=new File("d://test//导入.rar");
        File file=new File("d://test//test.zip");
        ExtractUtils.extractZipFiles(file);
    }
}
